﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace IntoNails.Models
{
    [Serializable]
    public abstract class AbstractModel<TKey> : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        #region Fields

        private string _displayName;
        private DateTime _createdDate;
        private DateTime _updatedDate;
        private bool _isValid;

        #endregion

        #region Constructors

        protected AbstractModel()
        {
            this.CreatedDate = DateTime.Now;
            this.UpdatedDate = DateTime.Now;
            this.IsValid = true;
        }

        #endregion

        #region Properties

        public TKey UniqueCode { get; protected set; }

        public string DisplayName
        {
            get { return this._displayName; }
            set
            {
                this._displayName = value;
                this.RaisePropertyChanged("DisplayName");
            }
        }

        public DateTime CreatedDate
        {
            get { return this._createdDate; }
            set
            {
                this._createdDate = value;
                this.RaisePropertyChanged("CreatedDate");
            }
        }

        public DateTime UpdatedDate
        {
            get { return this._updatedDate; }
            set
            {
                this._updatedDate = value;
                this.RaisePropertyChanged("UpdatedDate");
            }
        }

        public bool IsValid
        {
            get { return this._isValid; }
            set
            {
                this._isValid = value;
                this.RaisePropertyChanged("IsValid");
            }
        }

        #endregion
        
        #region Implementation of IDataErrorInfo

        public string this[string columnName]
        {
            get { return columnName; }
        }

        public string Error
        {
            get { return string.Empty; }
        }

        #endregion

        protected void RaisePropertyChanged(string propertyName)
        {
            var onPropertyChanged = PropertyChanged;
            if (onPropertyChanged == null)
                return;

            onPropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
